package com.orhon.smartcampus.modules.base.mm;

import com.orhon.smartcampus.basicComponents.service.GQLRunService;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.gqlModel.GQLDictionary;
import com.orhon.smartcampus.modules.base.inputMapper.DictionaryInputMapper;
import com.orhon.smartcampus.utils.ObjectToMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

@Mapper(
		uses = {
				LangMapper.class
		}
		)
public interface DictionaryMapper{



	DictionaryMapper MAPPER = Mappers.getMapper(DictionaryMapper.class);

	@Mapping(source = "id",target = "id")
	@Mapping(source = "alias",target = "alias")
	//@Mapping(source = "valueCode",target = "code")
	@Mapping(source = "type",target = "type")
	@Mapping(source = "dictionaryName",target = "name")//lang
	@Mapping(source = "dictionaryDescription",target = "description")//lang
	@Mapping(source = "status",target = "status")
	@Mapping(source = "meta",target = "meta")//lang
	@Mapping(source = "valueCode",target = "value")
	@Mapping(source = "standardType",target = "standard_type")
	@Mapping(source = "dictionaryCode",target = "dictionary_code")
	@Mapping(source = "createdAt",target = "created_at")
	@Mapping(source = "updatedAt",target = "updated_at")
	@Mapping(source = "deletedAt",target = "deleted_at")
	@Mapping(source = "parent_id",target = "parent_id")
	@Mapping(source = "dictionaryValue",target = "dictionary_value")
	GQLDictionary toDictionary(Dictionary entity);

	@InheritConfiguration(name = "toDictionary")
	List<GQLDictionary> toDictionaryList(List<Dictionary> entities);

	@Named("str2dic")
	default GQLDictionary mapFromString(String inStr){
		HashMap<String,Object> map = GQLRunService.DictionaryMap();
		Dictionary object = (Dictionary) map.get(inStr);
		HashMap<String,Object> hashMap = ObjectToMap.to(object);
		GQLDictionary dictionary = DictionaryMapper.MAPPER.toDictionary(object);
		return dictionary;
	}

	@Named("StringToList")
	default List StringToList(String ids){
		if(ids==null||ids.equals("[]")) {
			return null;
		}
		String substring = ids.substring(1, ids.length()-1);
		if(substring.split(", ").length >0 || !substring.equals(null)) {
			ArrayList list = new ArrayList(); 
			String[] id = substring.split(", ");
			for (String string : id) {
				try {
					int i = Integer.parseInt(string);
					list.add(string);
				} catch (Exception e) {
					String string2 = string.substring(1, string.length()-1);
					list.add(string2);
				}
			}
			return list;
		}
		return null;
	}

	//    default GQLDictionary mapFromString2(String instr){
	//        return null;
	//    }
}
